package com.foreveross.fetl.application.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.IFetlDataApplication;
import com.foreveross.fetl.common.application.BaseApplicationImpl;
import com.foreveross.fetl.core.domain.freight.CrawlTaskTemplateEntity;
import com.foreveross.fetl.core.domain.freight.FetlDataEntity;
import com.foreveross.fetl.vo.CrawlTaskTemplateVO;
import com.foreveross.fetl.vo.FetlDataVO;

@Transactional
@Service("fetlDataApplication")
public class FetlDataApplicationImpl extends BaseApplicationImpl implements
		IFetlDataApplication {

	@Override
	public void save(FetlDataVO vo) {
		FetlDataEntity entity = new FetlDataEntity();
		try {
			entity.setAttachHbaseKey(vo.getAttachHbaseKey());
			entity.setFromCity(vo.getFromCity());
			entity.setToCity(vo.getToCity());
			entity.saveOrUpdate(entity);
		} catch (IllegalArgumentException e) {
			throw e;
		}
	}

	@Override
	public void delete(FetlDataVO vo) {
		new FetlDataEntity().getFetlDataRepository().createQuery("delete FetlDataEntity fde where fde.id= ?", new Object[]{vo.getId()}).executeUpdate();
	}

	@Override
	public FetlDataVO get(Integer id) {
		FetlDataEntity entity = new FetlDataEntity();
		FetlDataVO vo = new FetlDataVO();
		try {
			BeanUtils.copyProperties(vo, entity.get(id));
			return vo;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<FetlDataVO> find(String attachKey, String goTrip,
			String returnTrip) {
		List<FetlDataEntity> listEntity = new ArrayList<FetlDataEntity>();
		List<FetlDataVO> listVO = new ArrayList<FetlDataVO>();
		FetlDataEntity entity = new FetlDataEntity();
		listEntity = entity.findByCondition(attachKey, goTrip, returnTrip);
		if (null != listEntity && listEntity.size() > 0) {
			for (FetlDataEntity en : listEntity) {
				FetlDataVO vo = new FetlDataVO();
				try {
					BeanUtils.copyProperties(vo, en);
					vo.setId(en.getId());
					listVO.add(vo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return listVO;
	}

	@Override
	public Page<CrawlTaskTemplateVO> pageQuery(String hbaseKey,
			String fromCity, String toCity, int currentPage, int pageSize) {
		List<CrawlTaskTemplateVO> result = new ArrayList<CrawlTaskTemplateVO>();
		// 分页查出指定航线CrawlTaskTemplateEntity实体列表
		StringBuffer jpql = new StringBuffer(
				"select crawlTaskTemplate from CrawlTaskTemplateEntity crawlTaskTemplate where 1=1");
		List<Object> conditionVals = new ArrayList<Object>();
		if (hbaseKey != null && !hbaseKey.isEmpty()) {
			jpql.append(" and crawlTaskTemplate.attachHbaseKey =?");
			conditionVals.add(hbaseKey);
		}
		if (fromCity != null && !fromCity.isEmpty()) {
			jpql.append(" and crawlTaskTemplate.fromCity =?");
			conditionVals.add(fromCity);
		}
		if (toCity != null && !toCity.isEmpty()) {
			jpql.append(" and crawlTaskTemplate.toCity =?");
			conditionVals.add(toCity);
		}
		jpql.append(" order by crawlTaskTemplate.attachHbaseKey desc");
		Page<CrawlTaskTemplateEntity> pages = this.queryChannel1()
				.queryPagedResultByPageNo(jpql.toString(),
						conditionVals.toArray(), currentPage, pageSize);
		IFetlDataApplication fetlDataApplication = new FetlDataApplicationImpl();
		List<FetlDataVO> fetlDataVOs = fetlDataApplication.findAll();
		FetlDataVO fetlDataVO = new FetlDataVO();
		for (CrawlTaskTemplateEntity en : pages.getResult()) {
			CrawlTaskTemplateVO vo = new CrawlTaskTemplateVO();
			try {
				BeanUtils.copyProperties(vo, en);
				vo.setId(en.getId());
				vo.setIsSelected(0);
				for (Iterator<FetlDataVO> iterator = fetlDataVOs.iterator(); iterator
						.hasNext();) {
					fetlDataVO = iterator.next();
					if (en.getAttachHbaseKey().equals(
							fetlDataVO.getAttachHbaseKey())
							&& en.getFromCity()
									.equals(fetlDataVO.getFromCity())
							&& en.getToCity().equals(fetlDataVO.getToCity())) {
						vo.setIsSelected(1);
						break;
					}
				}
				result.add(vo);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return new Page<CrawlTaskTemplateVO>(pages.getCurrentPageNo(),
				pages.getTotalCount(), pages.getPageSize(), result);
	}

	@Override
	public List<FetlDataVO> findAll() {
		List<FetlDataEntity> listEntity = new ArrayList<FetlDataEntity>();
		List<FetlDataVO> listVO = new ArrayList<FetlDataVO>();
		FetlDataEntity entity = new FetlDataEntity();
		listEntity = (List<FetlDataEntity>) entity.findAll();
		if (null != listEntity && listEntity.size() > 0) {
			for (FetlDataEntity en : listEntity) {
				FetlDataVO vo = new FetlDataVO();
				try {
					BeanUtils.copyProperties(vo, en);
					listVO.add(vo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return listVO;
	}

}
